package com.youlu.campus.service.wechatpay.impl;

import com.alibaba.fastjson.JSON;
import com.mongodb.client.result.UpdateResult;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.WechatPayCache;
import com.youlu.campus.service.wechatpay.WechatPayCacheService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

@Slf4j
@Service
public class WechatPayCacheServiceImpl implements WechatPayCacheService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Resource(name = "masterMongoTemplate")
    private MongoTemplate masterMongoTemplate;

    @Override
    public WechatPayCache create(WechatPayCache data) {
        log.info(":>>> 开始新增微信支付订单号缓存:{}", JSON.toJSONString(data));
        data.setCreatedTime(new Date());
        data.setStatus(Boolean.FALSE);
        data = mongoTemplate.insert(data);
        log.info(":>>> 新增的微信支付订单号缓存为:{}", JSON.toJSONString(data));
        return data;
    }

    @Override
    public String getBizType(String orderNo) {
        log.info(":>>> 开始查询微信支付业务类型:{}", orderNo);
        if (StringUtils.isBlank(orderNo)) {
            log.error(":>>> 订单编号为空");
            throw new BusinessException("订单编号为空");
        }
        WechatPayCache d = masterMongoTemplate.findOne(new Query().addCriteria(Criteria.where("orderNo").is(orderNo)), WechatPayCache.class);
        if (Objects.isNull(d)) {
            log.error(":>>> 微信支付业务类型不存在:{}", orderNo);
            //主从原因优先是H5情况，后面优化
            return "mix";
//            throw new BusinessException("微信支付业务类型不存在");
        }
        return d.getBizType();
    }

    @Override
    public WechatPayCache findByOrderNo(String orderNo) {
        log.info(":>>> 开始查找支付缓存:{}", orderNo);
        if (StringUtils.isBlank(orderNo)) {
            log.error(":>>> 订单编号为空");
            throw new BusinessException("参数错误");
        }
        Query query = new Query().addCriteria(Criteria.where("orderNo").is(orderNo));
        WechatPayCache wechatPayCache = mongoTemplate.findOne(query, WechatPayCache.class);
        log.info(":>>> 支付信息为:{}", JSON.toJSONString(wechatPayCache));
        return wechatPayCache;
    }

    @Override
    public boolean updateStatus(String orderNo, Boolean status) {
        log.info(":>>> 开始更新订单支付状态:{},{}", orderNo, status);
        if (StringUtils.isBlank(orderNo) || Objects.isNull(status)) {
            log.error(":>>> 订单编号或状态为空");
            throw new BusinessException("参数错误");
        }
        Query query = new Query().addCriteria(Criteria.where("orderNo").is(orderNo));
        Update update = new Update();
        update.set("status", status);
        UpdateResult updateResult = masterMongoTemplate.updateFirst(query, update, WechatPayCache.class);
        return updateResult.getModifiedCount() > 0 ? true : false;
    }

    @Override
    public boolean getStatus(String orderNo) {
        log.info(":>>> 开始查询订单:{}状态", orderNo);
        if (StringUtils.isBlank(orderNo)) {
            log.error(":>>> 订单编号或状态为空");
            throw new BusinessException("参数错误");
        }
        Query query = new Query().addCriteria(Criteria.where("orderNo").is(orderNo).and("status").is(Boolean.TRUE));
        boolean exist = mongoTemplate.exists(query, WechatPayCache.class);
        log.info(":>>> 订单:{} 已经支付状态:{}", orderNo, exist);

        return exist;
    }
}
